package MicroSpring.builder;

import MicroSpring.beanFactory.impl.BeanDefinition;
import MicroSpring.common.StringUtils;

/*
    bean名构造器
 */
public class BeanNameGenerator {

    public String generateBeanName(String beanName, BeanDefinition definition) {
        return StringUtils.hasText(beanName)?beanName:this.generateBeanName(definition);
    }
    public String generateBeanName(BeanDefinition definition) {
        String shortClassName = definition.getShortClassName();
        if (!StringUtils.hasText(shortClassName)) {
            return "";
        }
        String beanName=decapitalize(shortClassName);
        return beanName;
    }

    public static String decapitalize(String name) {
        if (name == null || name.length() == 0) {
            return name;
        }
        if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) &&
                Character.isUpperCase(name.charAt(0))){
            return name;
        }
        char chars[] = name.toCharArray();
        chars[0] = Character.toLowerCase(chars[0]);
        return new String(chars);
    }
}
